package benlai_miniprogram.benlaiApi;

import benlai_miniprogram.HttpGlobal;
import com.alibaba.fastjson.JSONObject;
import ocg.common.re.Assertion;
import ocg.http.httpcore.Http;
import ocg.http.httpcore.HttpFactory;
import ocg.http.httpcore.HttpModel;
import ocg.http.httpcore.HttpResponse;

/**
 * Created by chenbo on 2018/5/9.
 */
public class Location_Get{

    public static JSONObject value;

    /**
     *
     {
         "code": 0,
         "msg": "success",
         "value": {
             "success": true,
             "country": "中国",
             "province": "上海市",
             "provinceSysNo": 26,
             "city": "上海市",
             "citySysNo": 120,
             "district": "徐汇区",
             "districtSysNo": 1092,
             "webSiteSysNo": 3
             }
     }
     */
    public static void location_get( String j_E ,String w_N ){
        HttpModel model = Http.get ( HttpGlobal.Path.Location_Get )
                .parameter ( "longitude", j_E )
                .parameter ( "latitude", w_N )
                .parameters ( HttpGlobal.getParameters () );

        HttpResponse response = HttpFactory.factory ( model );
        JSONObject object = response.jsonBody ();

        int code = object.getIntValue ( "code" );
        String msg = object.getString ( "msg" );
        value = object.getJSONObject ( "value" );

        if ( value != null) {
            String city = value.getString ( "city" );
            String citySysNo = value.getString ( "citySysNo" );
            String webSiteSysNo = value.getString ( "webSiteSysNo" );

            HttpGlobal.setCitySysNo ( citySysNo );
            HttpGlobal.setWebSiteSysNo ( webSiteSysNo );
            HttpGlobal.setStorage ( "city",city );

            Assertion.isEquals ( value.getBoolean ( "success" ) , true , "定位成功" );
        }else {
            Assertion.isEquals ( msg , "success" , "Api error: " + code + " __ " );
        }

    }
}
